var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'S', 'P', 'R', 'U', 'V', 'X', 'Y'];

function initChartChoice() {
    $(".chart-choice").empty();
    var NnumBerColumn = 0;
    if (Program.data_question != undefined) {
        if (Program.other_opinion == 1) {
            NnumBerColumn = parseInt(Object.keys(Program.data_question.choice).length) + 1;
        } else {
            NnumBerColumn = Object.keys(Program.data_question.choice).length;
        }
        initCharts(NnumBerColumn, function () {
            socket.emit('get-analytic', Program.id_question, Program.id_tvshow);
        });
    }

}

function updateChart(data) {
    for (var i = 1; i <= data.length; i++) {
        var pt = data[(i - 1)];
        $(".show-analytic-" + i).html(pt + '%');
        $(".show-analytic-" + i).animate({
            width: pt + '%'
        }, 100);
    }
}

function format_number(dec, fix) {
    fixValue = parseFloat(Math.pow(10, fix));
    retValue = parseInt(Math.round(dec * fixValue)) / fixValue;
    return retValue;
}

function initCharts(num, success) {
    var html = '';
    for (var i = 1; i <= num; i++) {
        html += '<li class="answer choice-' + i + '"><label>' + i + '</label><div class="percent show-analytic-' + i + '" style="width: 0%">0%</div></li>';
    }
    $(".Statistic").empty().removeClass("zoomIn animated");
    setTimeout(function () {
        $(".Statistic").html(html).addClass("zoomIn animated");
        success();
    }, 2000);
}

Program.runSocketStatistic = function () {

    socket.on('receiveAnalytic', function (newdata) {
        var ids_show = Program.id_tvshow;

        if (newdata.id_tvshow == ids_show) {
            var data = newdata.vote;
            var length_ = data.length;
            var _data = [];
            var count_rs = 0;
            for (var iz = 1; iz < length_; iz++) {
                var npt = (data[iz] * 100) / newdata.tanalyticsRoom;
                var n = parseFloat(npt);
                var pt = format_number(n, 1);
                _data.push(pt);
            }
            updateChart(_data);
        }

    });

    socket.on('response-get-analytic', function (data_) {
        //console.log("===response-get-analytic===");
        var ids_show = Program.id_tvshow;
        var id_tvshow_ = data_.id_tvshow;
        if (id_tvshow_ == ids_show) {

            var data = data_.analytics;

            var length_ = data.length;
            var _data = [];
            //var count_rs = 0;
            for (var iz = 1; iz < length_; iz++) {
                var npt = (data[iz] * 100) / data_.data2;
                var n = parseFloat(npt);
                var pt = format_number(n, 1);
                _data.push(pt);
            }
            updateChart(_data);
        }
    });
}
